package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class COMDCB extends AxdrType {
    private byte[] dataCode = null;
    private AxdrEnum baut = null;
    private AxdrEnum parity = null;
    private AxdrEnum datab = null;
    private AxdrEnum stopb = null;
    private AxdrEnum flow = null;

    public COMDCB() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public AxdrEnum getBaut() {
        return baut;
    }

    public AxdrEnum getParity() {
        return parity;
    }

    public AxdrEnum getDatab() {
        return datab;
    }

    public AxdrEnum getStopb() {
        return stopb;
    }

    public AxdrEnum getFlow() {
        return flow;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(AxdrEnum baut, AxdrEnum parity, AxdrEnum datab, AxdrEnum stopb, AxdrEnum flow) {
        this.baut = baut;
        this.parity = parity;
        this.datab = datab;
        this.stopb = stopb;
        this.flow = flow;
    }

    public void setBaut(AxdrEnum baut) {
        this.baut = baut;
    }

    public void setParity(AxdrEnum parity) {
        this.parity = parity;
    }

    public void setDatab(AxdrEnum datab) {
        this.datab = datab;
    }

    public void setStopb(AxdrEnum stopb) {
        this.stopb = stopb;
    }

    public void setFlow(AxdrEnum flow) {
        this.flow = flow;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.baut = new AxdrEnum();
        this.baut.setConst();
        codeLength += this.baut.decode(input);
        this.parity = new AxdrEnum();
        this.parity.setConst();
        codeLength += this.parity.decode(input);
        this.datab = new AxdrEnum();
        this.datab.setConst();
        codeLength += this.datab.decode(input);
        this.stopb = new AxdrEnum();
        this.stopb.setConst();
        codeLength += this.stopb.decode(input);
        this.flow = new AxdrEnum();
        this.flow.setConst();
        codeLength += this.flow.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.baut != null && this.parity != null && this.datab != null && this.stopb != null && this.flow != null) {
                codeLength = 0;
                codeLength += this.flow.encode(output);
                codeLength += this.stopb.encode(output);
                codeLength += this.datab.encode(output);
                codeLength += this.parity.encode(output);
                codeLength += this.baut.encode(output);
            } else {
                throw new IllegalStateException("flow, stopb, datab, parity, or baut is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.baut != null && this.parity != null && this.datab != null && this.stopb != null && this.flow != null) {
            return "sequence: {" + "baut: " + this.baut + ", parity: " + this.parity + ", datab: " + this.datab + ", stopb: " + this.stopb + ", flow: " + this.flow + "}";
        } else {
            return "flow, stopb, datab, parity, or baut is null";
        }
    }
}



